using System;
using NUnit.Framework;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ProgrammingLanguageNr1
{
	[TestFixture()]
	public class ASTCreator_TEST
	{
		static ErrorHandler s_errorHandler = new ErrorHandler();
		
		[Test()]
		public void CreatingSimpleTree ()
		{
			Tokenizer tokenizer = new Tokenizer(File.OpenText("code4.txt"), s_errorHandler);
			List<Token> tokens = tokenizer.getTokens();			
			
			Parser parser = new Parser(tokens, s_errorHandler);
			parser.process();
			
			//Console.WriteLine("Tree: " + parser.getAST().toStringTree());
			
			AST root = parser.getAST();
			Assert.AreEqual(Token.TokenType.STATEMENT_LIST, root.getTokenType());
			
			AST multiplicationTree = root.getChild(0);
			Assert.AreEqual(Token.TokenType.OPERATOR, multiplicationTree.getTokenType());
			
			AST operand1 = multiplicationTree.getChild(0);
			AST operand2 = multiplicationTree.getChild(1);
			Assert.AreEqual("a", operand1.getTokenString());
			Assert.AreEqual("b", operand2.getTokenString());
		}
		
		[Test()]
		public void CreatingSlightlyMoreComplicatedTrees ()
		{
			Tokenizer tokenizer = new Tokenizer(File.OpenText("code5.txt"), s_errorHandler);
			List<Token> tokens = tokenizer.getTokens();			
			
			Parser parser = new Parser(tokens, s_errorHandler);
			parser.process();
			
			//Console.WriteLine("Tree: " + parser.getAST().toStringTree());
			
			string expectedTree = "(<STATEMENT_LIST> (<VAR_DECL> float temp) (= temp (+ 1 (+ (* 2 3) 4))) (<DECLARATION_AND_ASSIGNMENT> (<VAR_DECL> int answer) (= answer (- (/ 100 10) 5))) (if (> 4 5) (<STATEMENT_LIST> (print ja)) (<STATEMENT_LIST> (print nej))))";
			Assert.AreEqual(expectedTree, parser.getAST().getTreeAsString());
		}
		
		[Test()]
		public void OperationOrder ()
		{
			Tokenizer tokenizer = new Tokenizer(File.OpenText("code6.txt"), s_errorHandler);
			List<Token> tokens = tokenizer.getTokens();			
			
			Parser parser = new Parser(tokens, s_errorHandler);
			parser.process();
			
			//Console.WriteLine("Tree: " + parser.getAST().toStringTree());
			
			Assert.AreEqual("(<STATEMENT_LIST> (+ (* a b) (+ (* c d) e)))",
				parser.getAST().getTreeAsString());
		}
		
		[Test()]
		public void ParenthesisBasics ()
		{
			Tokenizer tokenizer = new Tokenizer(File.OpenText("code7.txt"), s_errorHandler);
			List<Token> tokens = tokenizer.getTokens();			
			
			Parser parser = new Parser(tokens, s_errorHandler);
			parser.process();
			
			//Console.WriteLine("Tree: " + parser.getAST().toStringTree());
			
			Assert.AreEqual("(<STATEMENT_LIST> (* a (/ (+ b c) d)))", parser.getAST().getTreeAsString());
		}
		
		[Test()]
		public void ComplexExpressions ()
		{
			Tokenizer tokenizer = new Tokenizer(File.OpenText("code8.txt"), s_errorHandler);
			List<Token> tokens = tokenizer.getTokens();			
			
			Parser parser = new Parser(tokens, s_errorHandler);
			parser.process();
			
			//Console.WriteLine("Tree: " + parser.getAST().getTreeAsString());
		}
		
		[Test()]
		public void FunctionDeclaration ()
		{
			Tokenizer tokenizer = new Tokenizer(File.OpenText("code14.txt"), s_errorHandler);
			List<Token> tokens = tokenizer.getTokens();			
			
			Parser parser = new Parser(tokens, s_errorHandler);
			parser.process();
			
			//Console.WriteLine("Tree: " + parser.getAST().getTreeAsString());
			
			string expectedOutput = "(<STATEMENT_LIST> (<FUNC_DECL> int funky (<PARAMETER_LIST> (<PARAMETER> float a) (<PARAMETER> float b)) <STATEMENT_LIST>))";
			Assert.AreEqual(expectedOutput, parser.getAST().getTreeAsString());
		}
		
		[Test()]
		public void IfThenElseTree ()
		{
			Tokenizer tokenizer = new Tokenizer(File.OpenText("code35.txt"), s_errorHandler);
			List<Token> tokens = tokenizer.getTokens();			
			
			Parser parser = new Parser(tokens, s_errorHandler);
			parser.process();
			
			//Console.WriteLine("Tree: " + parser.getAST().getTreeAsString());
			
			string expectedOutput = "(<STATEMENT_LIST> (if a (<STATEMENT_LIST> b) (<STATEMENT_LIST> c)))";
			Assert.AreEqual(expectedOutput, parser.getAST().getTreeAsString());
		}
	}
}

